local chanlian = fk.CreateSkill{
  name = "doublez__chanlian"
}

Fk:loadTranslationTable{
  ["doublez__chanlian"] = "缠恋",
  [":doublez__chanlian"] = "出牌阶段，你可以令一名角色将本回合弃牌堆的两张牌置于牌堆顶，视为使用一张【决斗】或【杀】，"..
    "然后本阶段此技能需要回合弃牌堆恰好两张红色牌才可发动。",

  ["#doublez__chanlian"] = "缠恋：你可以令一名角色视为使用一张【决斗】或【杀】",
  ["#doublez__chanlian_choose"] = "缠恋：选择置于牌堆顶的牌",
  ["#doublez__chanlian_put"] = "%from 将弃牌堆中 %arg 张牌 %arg2 和 %arg3 置于牌堆顶",
  
  ["@@doublez__chanlian-turn"] = "缠恋",

  ["$doublez__chanlian1"] = "还望将军，稍等片刻。",
  ["$doublez__chanlian2"] = "将军，请留步。",
}

chanlian:addEffect("active", {
  can_use = function (self, player)
    if player:getMark("@@doublez__chanlian-turn") == 0 then
      return #player:getTableMark(chanlian.name.."-turn") >= 2
    end
    local cards = table.filter(table.map(player:getTableMark(chanlian.name.."-turn"), Util.Id2CardMapper), function (card)
      return Fk:currentRoom():getCardArea(card) == Card.DiscardPile
    end)
    return #cards == 2 and cards[1].color == Card.Red and cards[2].color == Card.Red
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    return #selected == 0
  end,
  card_filter = Util.FalseFunc,
  on_use= function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local cards = table.filter(player:getTableMark(chanlian.name.."-turn"), function (card)
      return Fk:currentRoom():getCardArea(card) == Card.DiscardPile
    end)
    if player:getMark("@@doublez__chanlian-turn") == 0 then
      cards = room:askToCards(target, {
        prompt = "#doublez__chanlian_choose",
        pattern = ".|.|.|.|.|.|"..table.concat(cards, ","),
        expand_pile = cards,
        max_num = 2,
        min_num = 2,
        cancelable = false,
        skill_name = chanlian.name 
      })
    end
    room:moveCardTo(cards, Card.DrawPile, nil, fk.ReasonPut, chanlian.name, nil, true, target)
    room:sendLog{
      type = "#doublez__chanlian_put",
      from = target.id,
      arg = 2,
      arg2 = Fk:getCardById(cards[1]),
      arg3 = Fk:getCardById(cards[2])
    }
    local duel, slash = Fk:cloneCard("duel"), Fk:cloneCard("slash")
    duel.skillName = chanlian.name
    slash.skillName = chanlian.name
    if not (target:canUse(duel, {bypass_times = true}) or target:canUse(slash, {bypass_times = true})) then return end
    room:askToUseVirtualCard(target, {
      name = {"slash", "duel"},
      skill_name = chanlian.name,
      cancelable = false,
      extra_data = {
        bypass_times = true,
      }
    })
    room:addPlayerMark(player, "@@doublez__chanlian-turn", 1)
  end
})

chanlian:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(chanlian.name, true) then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player.room.discard_pile, info.cardId) then
              return true
            end
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DiscardPile and not table.contains(player.room.discard_pile, info.cardId) then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local ids = player:getTableMark("doublez__chanlian-turn")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DiscardPile and not table.contains(room.discard_pile, info.cardId) then
          table.removeOne(ids, info.cardId)
        end
      end
    end
    room:setPlayerMark(player, "doublez__chanlian-turn", ids)
  end,
})

chanlian:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local ids = {}
  room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end
  end, Player.HistoryTurn)
  room:setPlayerMark(player, "doublez__chanlian-turn", ids)
end)

return chanlian